package net.entelijan.cobean.text.converter;

import net.entelijan.cobean.text.ITextConverter;
import net.entelijan.cobean.text.IllegalTextException;

public class LongTextConverter implements ITextConverter<Long> {

	private boolean nullable = true;

	public LongTextConverter() {
		super();
	}

	public LongTextConverter(boolean nullable) {
		super();
		this.nullable = nullable;
	}

	@Override
	public Long convertToObject(String stringValue) throws IllegalTextException {
		try {
			Long re;
			if (nullable) {
				re = null;
			} else {
				re = 0L;
			}
			if (stringValue != null && !"".equals(stringValue)) {
				re = Long.valueOf(stringValue);
			}
			return re;
		} catch (NumberFormatException e) {
			throw new IllegalTextException("'" + stringValue
					+ "' is not a valid integer");
		}
	}

	@Override
	public String convertToString(Long object) {
		if (object == null) {
			return "";
		}
		return "" + object;
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

}
